/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.inf.ufg.view.cadastros;

import br.inf.ufg.model.Areas;
import br.inf.ufg.model.AreasDAO;
import br.inf.ufg.model.Eventos;
import br.inf.ufg.model.EventosDAO;
import br.inf.ufg.model.Local;
import br.inf.ufg.model.LocalDAO;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author umarley.faria
 */
public class TelaCadastroEventos extends javax.swing.JFrame {

    /**
     * Creates new form TelaCadastroLocal
     */
    MaskFormatter formatadata, formatahora;

    public TelaCadastroEventos() {
        initComponents();
        iniciarBD();
        montarListaLocal();
        this.setResizable(false);
        this.setLocationRelativeTo(this);
        tab_DadosArea.enable(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btFirst = new javax.swing.JButton();
        btPrev = new javax.swing.JButton();
        btNext = new javax.swing.JButton();
        btLast = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btDeletar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        abas = new javax.swing.JTabbedPane();
        abaEvento = new javax.swing.JPanel();
        tfCodigo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        ckAtivo = new javax.swing.JCheckBox();
        ckAssentosReservados = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        cbLocal = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        try{
            formatahora = new MaskFormatter("##/##/####");
        }catch(Exception erro) {
            JOptionPane.showMessageDialog(null, "Formato de horas inválido:" + erro);
        }
        tfHora = new JFormattedTextField(formatahora);
        jLabel4 = new javax.swing.JLabel();
        try{
            formatadata = new MaskFormatter("##/##/####");
        }catch(Exception erro) {
            JOptionPane.showMessageDialog(null, "Formato de data inválido:" + erro);
        }
        tfData = new JFormattedTextField(formatadata);
        ;
        abaAreas = new javax.swing.JPanel();
        jToolBar2 = new javax.swing.JToolBar();
        tab_DadosArea = new javax.swing.JTabbedPane();
        tb_ListaArea = new javax.swing.JScrollPane();
        gridAreas = new javax.swing.JTable();
        pn_DadosArea = new javax.swing.JPanel();
        tfCodigoArea = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfValorArea = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfDescricaoArea = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tfQuatidadeArea = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btRem = new javax.swing.JButton();
        btAdd = new javax.swing.JButton();
        btGravar = new javax.swing.JButton();
        abaIngressos = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Eventos");
        setAlwaysOnTop(true);

        jToolBar1.setRollover(true);

        btFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_first.png"))); // NOI18N
        btFirst.setToolTipText("Primeiro Registro");
        btFirst.setFocusable(false);
        btFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btFirst);

        btPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_previous.png"))); // NOI18N
        btPrev.setToolTipText("Anterior");
        btPrev.setFocusable(false);
        btPrev.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPrev.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btPrev);

        btNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_next.png"))); // NOI18N
        btNext.setToolTipText("Próximo");
        btNext.setFocusable(false);
        btNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btNext);

        btLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_last.png"))); // NOI18N
        btLast.setToolTipText("Último Registro");
        btLast.setFocusable(false);
        btLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btLast);
        jToolBar1.add(jSeparator1);

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/add.png"))); // NOI18N
        btNovo.setToolTipText("Novo");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jToolBar1.add(btNovo);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/disk.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.setEnabled(false);
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btSalvar);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/pencil.png"))); // NOI18N
        btEditar.setToolTipText("Editar");
        btEditar.setEnabled(false);
        btEditar.setFocusable(false);
        btEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });
        jToolBar1.add(btEditar);

        btDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/delete.png"))); // NOI18N
        btDeletar.setToolTipText("Remover");
        btDeletar.setFocusable(false);
        btDeletar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btDeletar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btDeletar);

        btPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/magnifier.png"))); // NOI18N
        btPesquisar.setToolTipText("Procurar");
        btPesquisar.setFocusable(false);
        btPesquisar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPesquisar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btPesquisar);

        abas.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                abasStateChanged(evt);
            }
        });
        abas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                abasMouseClicked(evt);
            }
        });

        tfCodigo.setEditable(false);

        jLabel1.setText("Código:");

        jLabel2.setText("Descrição:");

        ckAtivo.setText("Ativo");

        ckAssentosReservados.setText("Assentos Reservados?");

        jLabel6.setText("Local:");

        jLabel3.setText("Data:");

        try {
            tfHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel4.setText("Hora:");

        tfData.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfDataFocusLost(evt);
            }
        });
        tfData.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfDataKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfDataKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout abaEventoLayout = new javax.swing.GroupLayout(abaEvento);
        abaEvento.setLayout(abaEventoLayout);
        abaEventoLayout.setHorizontalGroup(
            abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaEventoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(abaEventoLayout.createSequentialGroup()
                        .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(abaEventoLayout.createSequentialGroup()
                                .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(abaEventoLayout.createSequentialGroup()
                                        .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 170, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, abaEventoLayout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfData, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfHora, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(abaEventoLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(cbLocal, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(136, 136, 136))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, abaEventoLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(ckAtivo)
                        .addGap(18, 18, 18)
                        .addComponent(ckAssentosReservados)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel6)
                        .addGap(375, 375, 375))))
        );
        abaEventoLayout.setVerticalGroup(
            abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaEventoLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(abaEventoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbLocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ckAtivo)
                    .addComponent(ckAssentosReservados))
                .addContainerGap(223, Short.MAX_VALUE))
        );

        abas.addTab("Evento", abaEvento);

        jToolBar2.setRollover(true);

        tab_DadosArea.setName(""); // NOI18N

        tb_ListaArea.setName(""); // NOI18N

        gridAreas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Valor", "Descricao", "Quantidade"
            }
        ));
        gridAreas.setEnabled(false);
        gridAreas.setFocusable(false);
        gridAreas.getTableHeader().setResizingAllowed(false);
        gridAreas.getTableHeader().setReorderingAllowed(false);
        tb_ListaArea.setViewportView(gridAreas);
        gridAreas.getAccessibleContext().setAccessibleName("");

        tab_DadosArea.addTab("Lista das áreas", tb_ListaArea);

        jLabel5.setText("Código");

        jLabel7.setText("Valor");

        jLabel8.setText("Descrição");

        jLabel9.setText("Quantidade");

        javax.swing.GroupLayout pn_DadosAreaLayout = new javax.swing.GroupLayout(pn_DadosArea);
        pn_DadosArea.setLayout(pn_DadosAreaLayout);
        pn_DadosAreaLayout.setHorizontalGroup(
            pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_DadosAreaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(tfCodigoArea, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(tfValorArea, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfDescricaoArea, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(tfQuatidadeArea, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        pn_DadosAreaLayout.setVerticalGroup(
            pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_DadosAreaLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pn_DadosAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfCodigoArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfValorArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDescricaoArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfQuatidadeArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(88, Short.MAX_VALUE))
        );

        tab_DadosArea.addTab("Dados", null, pn_DadosArea, "");

        btRem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/delete.png"))); // NOI18N
        btRem.setToolTipText("Remover");
        btRem.setFocusable(false);
        btRem.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btRem.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btRem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemActionPerformed(evt);
            }
        });

        btAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/add.png"))); // NOI18N
        btAdd.setToolTipText("Adicionar");
        btAdd.setFocusable(false);
        btAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btGravar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/disk.png"))); // NOI18N
        btGravar.setToolTipText("Remover");
        btGravar.setFocusable(false);
        btGravar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btGravar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGravarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btRem, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btGravar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(btAdd)
                .addGap(12, 12, 12)
                .addComponent(btRem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btGravar)
                .addContainerGap(64, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout abaAreasLayout = new javax.swing.GroupLayout(abaAreas);
        abaAreas.setLayout(abaAreasLayout);
        abaAreasLayout.setHorizontalGroup(
            abaAreasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaAreasLayout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(abaAreasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tab_DadosArea, javax.swing.GroupLayout.PREFERRED_SIZE, 595, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );
        abaAreasLayout.setVerticalGroup(
            abaAreasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaAreasLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(abaAreasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(abaAreasLayout.createSequentialGroup()
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tab_DadosArea, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(128, Short.MAX_VALUE))
        );

        tab_DadosArea.getAccessibleContext().setAccessibleDescription("Lista das áreas");

        abas.addTab("Areas", abaAreas);

        javax.swing.GroupLayout abaIngressosLayout = new javax.swing.GroupLayout(abaIngressos);
        abaIngressos.setLayout(abaIngressosLayout);
        abaIngressosLayout.setHorizontalGroup(
            abaIngressosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 668, Short.MAX_VALUE)
        );
        abaIngressosLayout.setVerticalGroup(
            abaIngressosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 323, Short.MAX_VALUE)
        );

        abas.addTab("Ingressos", abaIngressos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 473, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(abas, javax.swing.GroupLayout.PREFERRED_SIZE, 673, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(abas)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btNovoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed

        adicionarEvento();

    }//GEN-LAST:event_btSalvarActionPerformed


    private void tfDataKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfDataKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDataKeyPressed

    private void tfDataFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfDataFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDataFocusLost

    private void tfDataKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfDataKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDataKeyTyped

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btEditarActionPerformed

    private void abasStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_abasStateChanged
        // TODO add your handling code here:
        /*if (abas.getSelectedIndex()== 1){
         */
        if (abas.getSelectedIndex() == 1) {

            if (!tfNome.getText().isEmpty()){
            try {
                if (tfCodigo.getText().isEmpty()) {
                       insereAreas();
                } else {
                    if (!eventoDao.isEventoExiste(Integer.parseInt(tfCodigo.getText()))) {
                        insereAreas();
                    }
                }

            } catch (SQLException ex) {
                Logger.getLogger(TelaCadastroEventos.class.getName()).log(Level.SEVERE, null, ex);
            }
            }else{
                JOptionPane.showConfirmDialog(null,"Informe o evento.");
            }

        }


    }//GEN-LAST:event_abasStateChanged

    private void insereAreas() {

        adicionarEvento();
        try {
            int codigo = eventoDao.getUltimoEventoInserido();

            tfCodigo.setText(String.valueOf(codigo));

        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroEventos.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        //gridAreas.addColumn(null); //
        //        insereLinhaGrid();
        tab_DadosArea.setSelectedIndex(1);
        btAdd.setEnabled(false);
        btRem.setEnabled(false);
        btGravar.setEnabled(true);
        tfCodigoArea.setText("");
        tfValorArea.setText("");
        tfDescricaoArea.setText("");
        tfQuatidadeArea.setText("");
        tfCodigoArea.requestFocus();

    }//GEN-LAST:event_btAddActionPerformed

    private void btRemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btRemActionPerformed

    private void btGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGravarActionPerformed
        // TODO add your handling code here:
        btAdd.setEnabled(true);
        btRem.setEnabled(true);
        btGravar.setEnabled(false);
        tab_DadosArea.setSelectedIndex(0);

        try {

            String codigoArea = tfCodigoArea.getText();
            double valorArea = Double.parseDouble(tfValorArea.getText());
            String descricaoArea = tfDescricaoArea.getText();
            int quantidadeArea = Integer.parseInt(tfQuatidadeArea.getText());

            Areas area = new Areas();
            area.setEvento(Integer.parseInt(tfCodigo.getText()));
            area.setCodigo(codigoArea);
            area.setValor(valorArea);
            area.setDescricao(descricaoArea);
            area.setQuantidade(quantidadeArea);
            
            try {
                areaDao.salvarRegistro(area);
                
                preencher_jtable();
                
                //JOptionPane.showMessageDialog(this, "Evento adicionado com Sucesso");
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, "Erro: " + e.getMessage());
            }

        } catch (Exception e1) {
            JOptionPane.showMessageDialog(this, "Erro: " + e1.getMessage());
        }


    }//GEN-LAST:event_btGravarActionPerformed

    private void abasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_abasMouseClicked

    }//GEN-LAST:event_abasMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroEventos.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroEventos.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroEventos.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroEventos.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCadastroEventos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel abaAreas;
    private javax.swing.JPanel abaEvento;
    private javax.swing.JPanel abaIngressos;
    private javax.swing.JTabbedPane abas;
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btDeletar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btFirst;
    private javax.swing.JButton btGravar;
    private javax.swing.JButton btLast;
    private javax.swing.JButton btNext;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btPrev;
    private javax.swing.JButton btRem;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbLocal;
    private javax.swing.JCheckBox ckAssentosReservados;
    private javax.swing.JCheckBox ckAtivo;
    private javax.swing.JTable gridAreas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JPanel pn_DadosArea;
    private javax.swing.JTabbedPane tab_DadosArea;
    private javax.swing.JScrollPane tb_ListaArea;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JTextField tfCodigoArea;
    private javax.swing.JFormattedTextField tfData;
    private javax.swing.JTextField tfDescricaoArea;
    private javax.swing.JFormattedTextField tfHora;
    private javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfQuatidadeArea;
    private javax.swing.JTextField tfValorArea;
    // End of variables declaration//GEN-END:variables

    private LocalDAO localDao;
    private EventosDAO eventoDao;
    private AreasDAO areaDao;

    private void iniciarBD() {

        try {
            localDao = new LocalDAO();
            eventoDao = new EventosDAO();
            areaDao = new AreasDAO();
            
            
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Erro: " + e.getMessage());
        }
    }

    private void montarListaLocal() {

        try {
            List<Local> listaLocal = localDao.montarListaLocal();
            cbLocal.addItem("");
            for (Local local : listaLocal) {
                cbLocal.addItem(local.getNome());
            }

            //cbLocal.addItem("Cidade não Cadastrada");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "Erro: " + e.getMessage());
        }

    }

    private void insereLinhaGrid() {

        int linha = gridAreas.getModel().getRowCount();

        if (linha == 0) {
            linha = linha;
        } else {
            linha++;
        }

        System.out.println(linha);
        try {
            gridAreas.getModel().setValueAt("", linha, 0);
            gridAreas.getModel().setValueAt("", linha, 1);
            gridAreas.getModel().setValueAt("", linha, 2);
            gridAreas.getModel().setValueAt("", linha, 3);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }

    }

    private void adicionarEvento() {

        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);

        try {
            if (!tfCodigo.getText().isEmpty()) {
                int cod = Integer.parseInt(tfCodigo.getText());
            }

            String nome = tfNome.getText();
            String data = tfData.getText();
            String hora = tfHora.getText();
            int isAtivo = (ckAtivo.isSelected() ? 1 : 0);
            int local = localDao.getCodLocalByNome(
                    cbLocal.getSelectedItem().toString());
            int isAssentoReservado = (ckAssentosReservados.isSelected() ? 1 : 0);
            Eventos evento = new Eventos();
            // evento.setIdEvento(cod);
            evento.setNome(nome);
            evento.setData(data);
            evento.setHora(hora);
            evento.setLocal(local);
            evento.setIsAtivo(isAtivo);
            evento.setIsAssentoReservado(isAssentoReservado);
            try {
                eventoDao.salvarRegistro(evento);
                // JOptionPane.showMessageDialog(this, "Evento adicionado com Sucesso");
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, "Erro: " + e.getMessage());
            }

        } catch (Exception e1) {
            JOptionPane.showMessageDialog(this, "Erro: " + e1.getMessage());
        }
    }
    
    private void preencher_jtable()
    {
        gridAreas.getColumnModel().getColumn(0).setPreferredWidth(4);
        gridAreas.getColumnModel().getColumn(1).setPreferredWidth(12);
        gridAreas.getColumnModel().getColumn(2).setPreferredWidth(150);
        gridAreas.getColumnModel().getColumn(2).setPreferredWidth(4);
         
        DefaultTableModel modelo = (DefaultTableModel)gridAreas.getModel();
        modelo.setNumRows(0);
        
        try {
            List<Areas> listaAreas = areaDao.Areas();
                 //cbLocal.addItem("");
            for (Areas Areas : listaAreas) {
                modelo.addRow(new Object [] {Areas.getCodigo(),
                                             Areas.getValor(),
                                             Areas.getDescricao(),
                                             Areas.getQuantidade()});
            }
                
           // cbCidade.addItem("Cidade não Cadastrada");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "Erro: " + e.getMessage());
        }
    }
               
}
